package com.coachtam.ljgcapi;

import com.coachtam.ljgcapi.consumer.HystrixConsumer;
import com.coachtam.ljgcapi.consumer.RatelimiterConsumer;
import com.coachtam.ljgcapi.filter.RatelimiterGlobalFilter;
import com.coachtam.ljgcapi.filter.XssRequestGlobalFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.factory.HystrixGatewayFilterFactory;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

import javax.annotation.Resource;
import java.util.function.Consumer;

@SpringBootApplication
public class LjgcApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(LjgcApiApplication.class, args);
    }

    @Resource
    private HystrixConsumer hystrixConsumer;
//    @Resource
//    private RatelimiterConsumer ratelimiterConsumer;
    @Resource
    private XssRequestGlobalFilter xssRequestGlobalFilter;

    @Resource
    private RatelimiterGlobalFilter ratelimiterGlobalFilter;
    @Bean
    public RouteLocator reouteLocator(RouteLocatorBuilder builder) {

//        XssRequestGlobalFilter xssRequestGlobalFilter = new XssRequestGlobalFilter();
//        HystrixConsumer hystrixConsumer = new HystrixConsumer();
//        RatelimiterConsumer ratelimiterConsumer = new RatelimiterConsumer();
        return builder.routes()
                .route("path_route",
                        r -> r.path("/ljgcadmin/**")
                                .filters(f->
//                                        f.filter(ratelimiterGlobalFilter)
                                                f.hystrix(hystrixConsumer)

                                )
                                .uri("lb://ljgc-service"))
//                                .uri("http://localhost:9999"))
                .route("ljgc_route",
                        r->r.host("www.ljgc517.com")
                                .filters(f->
                                        f.filter(ratelimiterGlobalFilter)
                                         .filter(xssRequestGlobalFilter)
//                                        .setHostHeader("www.ljgc517.com")
                                        .hystrix(hystrixConsumer)).
                                uri("lb://ljgc-service"))
                .route("ljgcapp_route",
                        r->r.host("wx.ljgc517.com")
                                .filters(f->f.filter(xssRequestGlobalFilter)).
                                uri("http://localhost:8888"))

                .build();
    }
}
